﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdaptiveDeNet.RAND;

namespace AdaptiveDeNet.Functions
{
    public class Griewank : Function
    {
        private double[] o;

        public Griewank(double[] upperBound, double[] lowerBound)
        {
            o = new double[upperBound.Length];

            for (int i = 0; i < o.Length; i++)
            {
                o[i] = StaticRand.NextDouble() * (upperBound[i] - lowerBound[i]) + lowerBound[i];
            }
        }

        #region Function Members

        public double Feval(double[] x)
        {
            double fx = 0;

            double sum1 = 0;
            double mul1 = 1;

            for (int i = 0; i < x.Length; i++)
            {
                sum1 += (x[i] - o[i]) * (x[i] - o[i]);    
            }

            for (int i = 0; i < x.Length; i++)
            {
                mul1 *= Math.Cos((x[i] - o[i]) / Math.Sqrt(i + 1));    
            }

            fx = sum1 / 4000 - mul1 + 1;

            return fx;
        }

        #endregion



    }
}
